package com.example.springboot.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.example.springboot.common.Constants;
import com.example.springboot.common.Result;
import com.example.springboot.entity.Score;
import com.example.springboot.entity.Solution;
import com.example.springboot.exception.ServiceException;
import com.example.springboot.mapper.ScoreMapper;
import com.example.springboot.service.IScoreService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.springboot.service.ISolutionService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.text.DecimalFormat;

@Service
public class ScoreServiceImpl extends ServiceImpl<ScoreMapper, Score> implements IScoreService {
    @Resource
    private ScoreMapper scoreMapper;
    @Resource
    private IScoreService scoreService;
    @Resource
    private ISolutionService solutionService;

    @Override   //根据方案id查分
    public Double getScore(Integer solutionId) {
        Double scoreSum = scoreMapper.getScoreSum(solutionId);
        if (scoreSum == null) {
            return null;
        }
        else {
            double totalScore = scoreSum;
            double countScore = (double)scoreMapper.getScoreCount(solutionId);
            double score = totalScore / countScore;
            return Double.parseDouble(String.format("%.2f",score));
        }
    }

    @Override   //根据方案id和当前用户查分
    public Double getScoreByUserAndSolution(Integer userId,Integer solutionId) {
        QueryWrapper<Score> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("user_id",userId);
        queryWrapper.eq("solution_id",solutionId);
        Score score = scoreService.getOne(queryWrapper);
        if (score == null)
            return null;
        String s = new DecimalFormat("#.00").format(score.getScore());
        return Double.valueOf(s);
    }

    @Override//根据方案id查打分人数
    public Integer getCount(Integer solutionId) {
        return scoreMapper.getScoreCount(solutionId);
    }

    @Override
    public boolean deleteBySolution(Integer solutionId) {
        QueryWrapper<Score> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("solution_id",solutionId);
        return scoreService.remove(queryWrapper);
    }

    @Override
    public Result insertScore(Score score) {
        QueryWrapper<Score> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("user_id", score.getUserId());
        queryWrapper.eq("solution_id", score.getSolutionId());
        Score one;
        Solution solution;
        try {
            solution = solutionService.getById(score.getSolutionId());
            one = scoreService.getOne(queryWrapper);
        }catch (Exception e) {
            throw new ServiceException(Constants.CODE_500,"系统错误");
        }
        if (solution == null || !solution.getStatement().equals("已发布"))
            throw new ServiceException(Constants.CODE_600,"方案不存在");
        if (one == null)
            return Result.success(scoreService.save(score));
        else {
            return Result.success(scoreMapper.updateScore(score.getUserId(), score.getSolutionId(), score.getScore()));
        }
    }
}
